package com.fencing_squad.homo.list;

import com.bumptech.glide.Glide;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.util.ImageLoader;
import de.hdodenhof.circleimageview.CircleImageView;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class PostUserProvider extends BaseItemProvider {
    private List<PostUser> list;
    private Context context;
    private PostUserCard postUserCard;
    private static final int BEFORE_COUNT = 1;

    public PostUserProvider(Context context) {
        this.list = new ArrayList<>();
        this.context = context;
        this.postUserCard = new PostUserCard();
    }

    public PostUserProvider(Context context, String nickname, int post, int likes, int comment) {
        this.list = new ArrayList<>();
        this.context = context;
        this.postUserCard = new PostUserCard();
        postUserCard.setNickname(nickname);
        postUserCard.setLikes(likes);
        postUserCard.setComment(comment);
        postUserCard.setPost(post);
    }

    public PostUserProvider(Context context, PostUserCard postUserCard) {
        this.postUserCard = postUserCard;
        this.context = context;
    }

    public void setPostUserCard(PostUserCard postUserCard) {
        this.postUserCard = postUserCard;
        notifyDataChanged();
    }

    public class Holder {
        CircleImageView avatar;
        Text nickname;
        Text date;
        Text content;
        Text likes;
        Text comment;
        Image image;

        public Holder(Component component) {
            nickname = (Text) component.findComponentById(ResourceTable.Id_nickname);
            date = (Text) component.findComponentById(ResourceTable.Id_date);
            content = (Text) component.findComponentById(ResourceTable.Id_content);
            likes = (Text) component.findComponentById(ResourceTable.Id_likes);
            comment = (Text) component.findComponentById(ResourceTable.Id_comment);
            image = (Image) component.findComponentById(ResourceTable.Id_image);
            avatar = (CircleImageView) component.findComponentById(ResourceTable.Id_icon);
        }
    }

    public class HolderCard {
        CircleImageView icon;
        Text nickname;
        Text likes;
        Text comment;
        Text post;

        public HolderCard(Component component) {
            icon = (CircleImageView) component.findComponentById(ResourceTable.Id_icon);
            nickname = (Text) component.findComponentById(ResourceTable.Id_nickname);
            likes = (Text) component.findComponentById(ResourceTable.Id_likes);
            comment = (Text) component.findComponentById(ResourceTable.Id_comment);
            post = (Text) component.findComponentById(ResourceTable.Id_post);
        }
    }

    public void append(PostUser post) {
        if(list != null) {
            list.add(post);
            notifyDataChanged();
        }
    }

    public void append(List<PostUser> post) {
        if(list != null) {
            list.addAll(post);
            notifyDataChanged();
        }
    }

    @Override
    public int getCount() {
        return list == null ? BEFORE_COUNT : list.size() + BEFORE_COUNT;
    }

    @Override
    public Object getItem(int position) {
        if (position == 0) {
            return postUserCard;
        }
        else if (list != null && position >= BEFORE_COUNT && position < list.size() + BEFORE_COUNT){
            return list.get(position - BEFORE_COUNT);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        if (position == 0 && postUserCard != null) {
            final Component cpt;
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_user_post_card, null, false);
            HolderCard holder = new HolderCard(cpt);
            cpt.setTag(holder);
            holder.likes.setText(postUserCard.getLikes());
            holder.nickname.setText(postUserCard.getNickname());
            holder.comment.setText(postUserCard.getComment());
            holder.post.setText(postUserCard.getPost());
            Glide.with(context).load(postUserCard.getAvatar()).listener(ImageLoader.fail(holder.icon, ResourceTable.Media_icon)).into(holder.icon);
            return cpt;
        }
        else if (position >= BEFORE_COUNT && position < list.size() + BEFORE_COUNT) {
            final Component cpt;
            PostUser item = list.get(position - BEFORE_COUNT);
            Holder holder;
            if (convertComponent == null || !(convertComponent.getTag() instanceof Holder)) {
                cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_post_user, null, false);
                holder = new Holder(cpt);
                cpt.setTag(holder);
            } else {
                cpt = convertComponent;
                holder = (Holder) cpt.getTag();
            }
            holder.nickname.setText(item.getNickname());
            holder.date.setText(item.getDate());
            holder.content.setText(item.getContent());
            holder.likes.setText(item.getLikes());
            holder.comment.setText(item.getComment());
            Glide.with(context).load(item.getAvatar()).listener(ImageLoader.fail(holder.avatar, ResourceTable.Media_icon)).into(holder.avatar);
            if (!item.getImage().equals("")) {
                holder.image.setVisibility(Component.VISIBLE);
                Glide.with(context).load(item.getImage()).listener(ImageLoader.fail(holder.image, ResourceTable.Media_icon)).into(holder.image);
            }
            return cpt;
        }
        return null;
    }
}
